package com.huo.springboot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.LinkedHashSet;
import java.util.Set;

@Configuration
@PropertySource({"classpath:/application-redis.properties"} )
public class RedisClusterConfig {

    @Autowired
    private Environment environment;

    @Bean
    public JedisCluster jedisCluster(){
        //连接池的配置
        JedisPoolConfig jedisPool = new JedisPoolConfig();
        jedisPool.setMaxTotal( Integer.parseInt( environment.getProperty( "spring.redis.jedis.pool.max-active" )) );
        jedisPool.setMinIdle( Integer.parseInt( environment.getProperty( "spring.redis.jedis.pool.min-idle" ) ) );
        jedisPool.setMaxIdle( Integer.parseInt( environment.getProperty( "spring.redis.jedis.pool.max-idle" ) ) );
        jedisPool.setMaxWaitMillis( Long.parseLong( environment.getProperty( "spring.redis.jedis.pool.max-wait" ) ) );

        //添加redis连接节点
        Set<HostAndPort> hostAndPort = new LinkedHashSet<>( );
        hostAndPort.add( new HostAndPort( "192.168.1.121",7001) );
        hostAndPort.add( new HostAndPort( "192.168.1.121" ,7002) );
        hostAndPort.add( new HostAndPort( "192.168.1.121" ,7003) );
        hostAndPort.add( new HostAndPort( "192.168.1.121" ,7007) );
        hostAndPort.add( new HostAndPort( "192.168.1.122" ,7004) );
        hostAndPort.add( new HostAndPort( "192.168.1.122" ,7005) );
        hostAndPort.add( new HostAndPort( "192.168.1.122" ,7006) );
        hostAndPort.add( new HostAndPort( "192.168.1.122" ,7008) );
        //获取jediscluster对象
        JedisCluster cluster = new JedisCluster(hostAndPort,jedisPool);
        return cluster;

    }
}
